import json
import requests
import urllib3
import sys
from lxml import etree

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)


# 下载地址
class MP4so_Spider:
    def __init__(self, num):
        self.num = num
        self.url = "https://www.mp4so.com/detail/{}.html".format(num)
        self.headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36"}

    def parse_url(self, url):
        try:
            resp = requests.get(url, headers=self.headers, verify=False)
        except:
            return '无法访问'
        return resp.content.decode()

    def get_content_list(self, html_str):
        content_list = []
        html = etree.HTML(html_str)
        name = html.xpath("//h1/text()")[0]
        ul = html.xpath("//ul[@class='panel down-list thunder1']")[0]
        li_list = ul.xpath(".//li")

        for li in li_list:
            download_url = li.xpath(".//div[@class='url-left']/a/@href")
            download_url = download_url[0] if len(download_url) > 0 else None
            content_list.append(download_url)
        return name, content_list

    def save_content_list(self, name, content_list):
        with open(name + '.txt', 'w', encoding='utf-8') as f:
            for content in content_list:
                f.write(json.dumps(content, ensure_ascii=False, indent=2).replace("\"", ''))
                f.write('\n')

    def run(self):
        url = self.url
        html_str = self.parse_url(url)
        # self.get_content_list(html_str)
        try:
            name, content_list = self.get_content_list(html_str)
            self.save_content_list(name, content_list)
            print('写入完毕')
        except:
            print('不存在')


if __name__ == '__main__':
    name = sys.argv[1]
    eight_zero = MP4so_Spider(name)
    eight_zero.run()
